package cn.lili.modules.store.entity.dos;

import cn.hutool.core.text.CharSequenceUtil;
import cn.lili.common.utils.BeanUtil;
import cn.lili.common.utils.SnowFlake;
import cn.lili.modules.store.entity.dto.StoreApplyDTO;
import cn.lili.modules.store.entity.enums.StoreStatusEnum;
import cn.lili.mybatis.model.BaseSceneEntity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.io.Serial;
import java.util.Date;

/**
 * 店铺
 *
 * @author pikachu
 * @since 2020-02-18 15:18:56
 */
@Data
@TableName("li_store")
@ApiModel(value = "店铺")
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class Store extends BaseSceneEntity {

    @Serial
    private static final long serialVersionUID = -5861767726387892272L;


    @ApiModelProperty(value = "店铺名称")
    private String storeName;

    /**
     * @see StoreStatusEnum
     */
    @ApiModelProperty(value = "店铺状态")
    private String storeStatus;

    @ApiModelProperty(value = "是否自营")
    private Boolean selfOperated;

    @ApiModelProperty(value = "店长id")
    private String managerId;

    @ApiModelProperty(value = "店铺logo")
    private String storeLogo;

    @ApiModelProperty(value = "经纬度")
    private String storeCenter;

    @Size(min = 6, max = 200, message = "店铺简介需在6-200字符之间")
    @NotBlank(message = "店铺简介不能为空")
    @ApiModelProperty(value = "店铺简介")
    private String storeDesc;

    @ApiModelProperty(value = "描述评分")
    private Double descriptionScore;

    @ApiModelProperty(value = "服务评分")
    private Double serviceScore;

    @ApiModelProperty(value = "物流描述")
    private Double deliveryScore;

    @ApiModelProperty(value = "商品数量")
    private Integer goodsNum;

    @ApiModelProperty(value = "收藏数量")
    private Integer collectionNum;

    @Size(min = 18, max = 18, message = "营业执照长度为18位字符")
    @ApiModelProperty(value = "营业执照号")
    private String licenseNum;


    @NotBlank(message = "营业执照电子版不能为空")
    @ApiModelProperty(value = "营业执照电子版")
    private String licencePhoto;

    @NotBlank(message = "法人姓名不能为空")
    @Size(min = 2, max = 20, message = "法人姓名长度为2-20位字符")
    @ApiModelProperty(value = "法人姓名")
    private String legalPerson;

    @NotBlank(message = "法人身份证不能为空")
    @Size(min = 18, max = 18, message = "法人身份证号长度为18位")
    @ApiModelProperty(value = "法人身份证")
    private String legalPersonId;

    @NotBlank(message = "法人身份证正面")
    @ApiModelProperty(value = "法人身份证正面")
    private String legalPhoto1;

    @NotBlank(message = "法人身份证背面")
    @ApiModelProperty(value = "法人身份证背面")
    private String legalPhoto2;

    @NotBlank(message = "店铺经营类目不能为空")
    @ApiModelProperty(value = "店铺经营类目")
    private String businessCategory;

    @ApiModelProperty(value = "店铺经营许可证")
    private String businessLicense;

    @ApiModelProperty(value = "微信进件二级商户id")
    private String wechatSubMchid;

    @ApiModelProperty(value = "库存预警数量")
    private Integer stockWarning;

    @ApiModelProperty(value = "开启楼层装修")
    private Boolean enableDecoration = false;

    @ApiModelProperty(value = "开启自提")
    private Boolean enablePickup = false;
    @ApiModelProperty(value = "保留字段1", hidden = true)
    String field1 = "";

    @ApiModelProperty(value = "保留字段2", hidden = true)
    String field2 = "";

    @ApiModelProperty(value = "保留字段3", hidden = true)
    String field3 = "";

    @ApiModelProperty(value = "保留字段4", hidden = true)
    String field4 = "";

    @ApiModelProperty(value = "保留字段5", hidden = true)
    String field5 = "";

    @ApiModelProperty(value = "扩展字段，可自由存储，数据库为text格式", hidden = true)
    String ext = "";

    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @ApiModelProperty(value = "店铺关闭时间")
    private Date storeEndTime;

    @ApiModelProperty(value = "供应商所属城市分站ID")
    private String urbanUserId;

    @ApiModelProperty("站点名称")
    @TableField(exist = false)
    private String urbanUserName;

    @ApiModelProperty(value = "是否为盲和店铺  true/false 是/不是")
    private Boolean isBlindbox;

    @ApiModelProperty(value = "是否允许从1688采集商品")
    private Boolean grabAlibabaOpen = false;

    @ApiModelProperty("申请入驻人")
    private String enterId;

    @ApiModelProperty("审核驳回原因")
    private String rejectReason;

    @ApiModelProperty(value = "是否礼包店铺  true/false")
    private Boolean isGiftStore;

    @ApiModelProperty("是否免税店铺")
    private Boolean isFree;

    public Store(StoreApplyDTO storeApplyDTO) {
        BeanUtil.copyProperties(storeApplyDTO, this);

        setBusinessCategoryArray(storeApplyDTO.getBusinessCategory());

        this.stockWarning = 10;
        storeStatus = StoreStatusEnum.APPLYING.value();
        selfOperated = false;
        deliveryScore = 5.0;
        serviceScore = 5.0;
        descriptionScore = 5.0;
        goodsNum = 0;
        collectionNum = 0;
        if (storeName == null) {
            storeName = "小店" + SnowFlake.getIdStr();
        }

        if (CharSequenceUtil.isEmpty(getId())) {
            this.setId(SnowFlake.getIdStr());
            this.setExtendId(this.getId());
        }
    }

    public void setBusinessCategoryArray(String[] businessCategory) {
        if (businessCategory != null && businessCategory.length > 0) {
            this.businessCategory = CharSequenceUtil.join(",", businessCategory);
        }
    }

}